<?php

namespace App\Model;

use Illuminate\Support\Facades\DB;

/**
 * Class Commodity
 * @package App\Model
 * @User wuqin
 * @Date 2018/3/17 0017
 * @Time 13:25
 */
class Commodity extends Common
{
    /**
     * @var bool
     */
    public $timestamps = false;

    /**
     * @var string
     */
    protected $primaryKey = 'commodity_id';

    /**
     * @var string
     */
    protected $table = 'commoditys';

    public function get_all_commodity()
    {
        $key_index = "commodity:*";
        $field = ["commodity_id", "commodity_classify_ids", "commodity_name", "commodity_title", "index_img_url"];
        if (empty($keys = $this->keys_redis($key_index))) {
            $data = $this->get(array(), $field, 0, 0, ["commodity_id", "asc"]);
            if (empty($data))
                $this->set_empty_array_redis("commodity:0");
            $this->set_array($data);
        }
        $keys = $this->keys_redis($key_index);
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    /**
     * 方法 : get_commodity_by_commidity_id
     * @User wuqin
     * @DateTime 2018-03-27 19:40
     * @param $commodity_id
     * @return array|bool|mixed
     */
    public function get_commodity_by_commidity_id($commodity_id)
    {
        if (empty($commodity_id))
            return false;
        $key_index = 'commodity_all:' . $commodity_id . ':commodity_classify_ids:*';
        if (empty($keys = $this->keys_redis($key_index))) {
            $where = array(
                array(
                    'where', 'commodity_id', '=', $commodity_id
                )
            );
            $data = $this->get($where, '*', 0, 0);
            if (empty($data))
                $this->set_empty_array_redis('commodity_all:0:commodity_classify_ids:' . $commodity_id);
            $this->set_array_all($data);
        }
        $keys = $this->keys_redis($key_index);
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    public function get_commodity_by_commidity_ids(array $commodity_id)
    {
        if (empty($commodity_id) || !is_array($commodity_id))
            return false;
        $where = array(
            array(
                'whereIn', 'commodity_id', $commodity_id
            )
        );
        $field = ["commodity_id", "commodity_name", "commodity_title", "index_img_url"];

        $data = $this->get($where, $field, 0, 0);
        return $data;
    }

    /**
     * 方法 : set_array_all
     * @User wuqin
     * @DateTime 2018-03-27 19:40
     * @param $data
     * @return bool
     */
    public function set_array_all($data)
    {
        if (empty($data))
            return false;
        foreach ($data as $key => $value) {
            $this->set_array_redis('commodity_all:' . $value['commodity_id'] . ':commodity_classify_ids:' . $value['commodity_classify_ids'], $value);
        }
    }

    public function get_commodity_by_name($name)
    {
        if (empty($name))
            return false;

        $where = array(
            array("where", "commodity_name", "like", "%$name%"),
            array("orWhere", "commodity_title", "like", "%$name%"),
        );
        $fields = ["commodity_id", "commodity_classify_ids", "commodity_name", "commodity_title", "index_img_url"];
        $res = $this->get($where, $fields, 0, 0);

        if (empty($res))
            return false;
        else
            return $res;
    }

    /**
     * 方法 : get_commodity_by_classify_id
     * @User wuqin
     * @DateTime 2018-03-27 19:40
     * @param $classify_id
     * @return array|bool
     */
    public function get_commodity_by_classify_id($classify_id)
    {
        if (empty($classify_id))
            return false;
        $key_index = 'commodity:*:commodity_classify_ids*[:,]' . $classify_id . ',*';
        if (empty($keys = $this->keys_redis($key_index))) {
            $data = DB::select("select commodity_id,commodity_classify_ids,commodity_name,commodity_title,index_img_url from commoditys where commodity_classify_ids regexp '(,|^)?(,|^)'", [$classify_id]);
            $data = array_map('get_object_vars', $data);
            if (empty($data))
                $this->set_empty_array_redis('commodity:0:commodity_classify_ids:' . $classify_id);
            $this->set_array($data);
        }
        $keys = $this->keys_redis($key_index);
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    /**
     * 方法 : set_array
     * @User wuqin
     * @DateTime 2018-03-24 20:36
     * @param $data
     * @return bool
     */
    public function set_array($data)
    {
        if (empty($data))
            return false;
        foreach ($data as $key => $value) {
            $this->set_array_redis('commodity:' . $value['commodity_id'] . ':commodity_classify_ids:' . $value['commodity_classify_ids'], $value);
        }
    }

    /**
     * 方法 : add_commodity
     * @User wuqin
     * @DateTime 2018-03-17 13:25
     * @param $var
     * @return bool
     */
    public function add_commodity($var)
    {
        $this->commodity_classify_ids = $var['commodity_classify_ids'];
        $this->commodity_name = $var['commodity_name'];
        $this->commodity_title = $var['commodity_title'];
        $this->index_img_url = $var['index_img_url'];
        $this->commodity_desc = $var['commodity_desc'];
        if (!$this->save())
            return false;
//        $where = array(
//            array(
//                'where', 'commodity_id', '=', $this->commodity_id
//            )
//        );
//        $data = $this->toArray();
//        if (empty($data)) {
////            $this->set_empty_array_redis('commodity:' . $this->commodity_id . ':');
//            return false;
//        }
//        $this->set_array($data);
//        if (!empty($keys = $this->keys_redis('commodity:' . $this->commodity_id . ':*'))) {
//            return true;
//        }
        return true;
//        return false;
    }

    /**
     * 方法 : update_commodity
     * @User wuqin
     * @DateTime 2018-03-17 13:25
     * @param $var
     * @return bool
     */
    public function update_commodity($var)
    {
        $this->commodity_classify_ids = $var['commodity_classify_ids'];
        $this->commodity_name = $var['commodity_name'];
        $this->commodity_title = $var['commodity_title'];
        $this->index_img_url = $var['index_img_url'];
        $this->commodity_desc = $var['commodity_desc'];
        if (!$this->save())
            return false;
        $where = array(
            array(
                'where', 'commodity_id', '=', $this->commodity_id
            )
        );
        $data = $this->get($where, '*', 0, 0);
        if (empty($data)) {
            $this->set_empty_array_redis('commodity:' . $this->commodity_id . ':');
            return false;
        }
        $this->set_array($data);
        if (!empty($keys = $this->keys_redis('commodity:' . $this->commodity_id . ':*'))) {
            return true;
        }
        return false;
    }

    /**
     * 方法 : set_empty
     * @User wuqin
     * @DateTime 2018-03-24 20:36
     * @param string $commodity_id
     * @param string $commodity_id
     */
    public function set_empty($commodity_id = '0', $classify_id = '0')
    {
        $this->set_empty_array_redis('commodity:' . $commodity_id . ':commodity_classify_ids:' . $classify_id);
    }

    /**
     * 方法 : set_empty_all
     * @User wuqin
     * @DateTime 2018-03-27 19:40
     * @param string $commodity_id
     * @param string $classify_id
     */
    public function set_empty_all($commodity_id = '0', $classify_id = '0')
    {
        $this->set_empty_array_redis('commodity_all:' . $commodity_id . ':commodity_classify_ids:' . $classify_id);
    }
}
